package com.laoyao.controller;

import com.jfinal.core.Controller;
import com.laoyao.comm.config.CrossOrigin;
import com.laoyao.comm.model.Order;
import com.laoyao.comm.model.User;
import com.laoyao.util.DateUtil;
import com.laoyao.util.MD5Util;
import com.laoyao.util.MD5Util2;
import com.laoyao.util.abc;

@CrossOrigin
public class PayController extends Controller {

	public void index(){
		System.out.println("支付测试");
		String order_no = getPara("order_no");
		String money = getPara("money");
		int day = 30;
		
		if(money.equals("0.20"))
			day=365;
		if(money.equals("0.10"))
			day=90;
		
		Order o = Order.dao.findById(order_no);
		User u = User.dao.findById(o.getUserId());
		
		if(DateUtil.dateCmp(DateUtil.getTime(), u.getVipTime())==1){
			u.setVipTime(DateUtil.addDay(day));
		}else{
			u.setVipTime(DateUtil.addDay(u.getVipTime(), day));
		}
		o.setPayState(1);
		o.setPayTime(DateUtil.getTime());
		u.update();
		o.update();
		renderJson("success");
	}
	
	//支付参数
	public void payParam(){
		Order o = getBean(Order.class,"");
		if(o.getUserId()==null){
			renderJson("msg","未登录，请先登录后在开通会员。");
			return;
		}
		
		String order_no = DateUtil.getyyyyMMddHHmmssSSS()+abc.generateRandCode(2);
		String str = "order_no="+order_no;
		str+="&subject=会员开通";
		str+="&pay_type=43";
		str+="&money="+ String.format("%.2f", o.getMoney());
		str+="&app_id=10785";
		str+="&extra=";
		String sign = MD5Util2.encryption(str+"&6fa5c7cb5e274266b265140e27de7f1e");
		str+="&sign="+sign;
		o.setTime(DateUtil.getTime());
		o.setId(order_no);
		o.setSign(sign);
		o.save();
		renderJson("param",str);
	}
}
